package collection;

import groupby.manage.GroupbyManage;
import groupby.model.Groupby;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dic.manage.DicManage;
import dic.model.City;

public class LaShou {
	public static void startCollection(ApplicationContext context)
	{
		String urlStr="http://open.client.lashou.com/list/cities/";
		StringBuffer s = new StringBuffer();
		try {
			URL url = new URL(urlStr);
			try {
				SAXReader reader = new SAXReader();
				Document doc = reader.read(url);
				Element root = doc.getRootElement();
				
				List list = doc.selectNodes("//cities/city");
				for (Iterator it = list.iterator(); it.hasNext();) {
					Element element=(Element) it.next();
					Node city=element.selectSingleNode("cityid");
					addData(city.getText(),context);
				}
				
			} catch (Exception e) {

				e.printStackTrace();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void addData(String city,ApplicationContext context)
	{
		String urlStr="http://open.client.lashou.com/list/goods/cityid/"+city;
		GroupbyManage groupbyManage=(GroupbyManage)context.getBean("groupbyManage");
		StringBuffer s = new StringBuffer();
		try {
			URL url = new URL(urlStr);
			try {
				SAXReader reader = new SAXReader();
				Document doc = reader.read(url);
				Element root = doc.getRootElement();
				Groupby groupby=new Groupby();
				
				
				Node title = doc.selectSingleNode("//lashou/goods/goods_title");
				
				Node dealUrl = doc.selectSingleNode("//lashou/goods/goods_site_url");
				Node image = doc.selectSingleNode("//lashou/goods/goods_sp_url");
				Node cityId = doc.selectSingleNode("//lashou/cityname");
				
				Node startDate=doc.selectSingleNode("//lashou/goods/goods_start_time");
				Node endDate=doc.selectSingleNode("//lashou/goods/goods_deadline");
				Node price=doc.selectSingleNode("//lashou/goods/goods_price");
				Node originalCost=doc.selectSingleNode("//lashou/goods/goods_value");
				Node discountPercent=doc.selectSingleNode("//lashou/goods/goods_rebate");
				
				
				
				groupby.setTitle(title.getText());
				groupby.setDealUrl(dealUrl.getText());
				groupby.setImage(image.getText());
				
				DicManage dicManage=(DicManage)context.getBean("dicManage");
				City cityObj=dicManage.findCityByName(cityId.getText());
				if(cityObj==null||cityObj.getFlag()!=1)
				{
				    return ;
				}
				boolean isFind=groupbyManage.isFindGroupby(title.getText(),cityObj.getId());
				if(!isFind)
				{
					return ;
				}
				groupby.setCityId(cityObj.getId());
				
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date=new Date();
				
				date.setTime(Long.parseLong(startDate.getText()+"000"));
				groupby.setStartDate(sdf.format(date));
				date.setTime(Long.parseLong(endDate.getText()+"000"));
				groupby.setEndDate(sdf.format(date));
				
				groupby.setPrice(Float.parseFloat(price.getText()));
				groupby.setOriginalCost(Float.parseFloat(originalCost.getText()));
				groupby.setDiscountPercent(discountPercent.getText());
				groupby.setDiscountAmount(Float.parseFloat(originalCost.getText())-Float.parseFloat(price.getText()));
				groupby.setTopCount(0);
				groupby.setInputDatetime(sdf.format(new Date()));
				groupby.setWebsite("拉手网");
				
				
				groupbyManage.save(groupby);
			} catch (Exception e) {

				e.printStackTrace();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
}
